﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace milanov
{
    //Thom
    public partial class Category : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["category"] != null)
            {
                //Zet de waarde dat in de URL staat in een string variable
                string parameter = Request.QueryString["category"].ToString();
                //laad de bijhorende methode en geef deze string mee
                Loadparam(parameter);
            }
            else
                //als er niets is ingevuld laad dan de grid met alle categorien
                Loadnoparam();
        }

        public void Loadparam(string category)
        {
            using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["fotosDB"].ConnectionString))
            {

                string query = "SELECT * FROM [Fotos] WHERE Category = @cat";
                //Dit is een Code die ervoor zorgt dat de eerste letter een hoofdletter wordt
                cat.Text = category.First().ToString().ToUpper() + String.Join("", category.Skip(1));
                Page.Title = category.First().ToString().ToUpper() + String.Join("", category.Skip(1));
                using (SqlDataAdapter da = new SqlDataAdapter(query, con))
                {
                    //Zet de category parameter aan @cat zodat dit in de query wordt gezet
                    da.SelectCommand.Parameters.AddWithValue("@cat", category);
                    DataTable dt = new DataTable();
                    da.Fill(dt);
                    GridView1.DataSource = dt;
                    GridView1.DataBind();
                }
            }
        }
        public void Loadnoparam()
        {
            using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["fotosDB"].ConnectionString))
            {
                con.Open();
                string query = "SELECT * FROM [Fotos]";
                using (SqlDataAdapter da = new SqlDataAdapter(query, con))
                {
                    DataTable dt = new DataTable();
                    da.Fill(dt);
                    GridView1.DataSource = dt;
                    GridView1.DataBind();
                }
            }
        }
        /// <summary>
        /// Deze methode zorgt ervoor dat je op de rows van de gridview kan klikken om de volledige afbeelding te bekijken.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {

                string File = DataBinder.Eval(e.Row.DataItem, "Filename").ToString();
                //string Location = ResolveUrl("~/Details.aspx") + "?ProductId=" + Filename;
                string Location = ResolveUrl("~/Pictures/demo/") + "demo_" + File;
                e.Row.Attributes["onClick"] = string.Format("javascript:window.location='{0}';", Location);
                e.Row.Style["cursor"] = "pointer";
            }
        }
        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView1.PageIndex = e.NewPageIndex;
            GridView1.DataBind();
        }
    }
}